package com.liushuai.phoneguardian;

import com.liushuai.phoneguardian.adapter.HomeAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class HomeActivity extends Activity implements OnClickListener {
	private GridView gv_home_avtivity;
	private static final String TAG = "HomeActivity";

	SharedPreferences sp;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		gv_home_avtivity = (GridView) findViewById(R.id.gv_home_activity);
		gv_home_avtivity.setAdapter(new HomeAdapter(this));
		gv_home_avtivity.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				switch (position) {
				case 0:
					Log.i(TAG, "点击了手机防盗");
					if (isSetPwd()) {
						showNomarlDialog();
					} else {
						showFirstDialog();
					}
					break;
				case 1:
					Log.i(TAG, "点击了通讯卫士");
					break;
				case 2:
					Log.i(TAG, "点击了软件管理");
					break;
				case 3:
					Log.i(TAG, "点击了进程管理");
					break;
				case 4:
					Log.i(TAG, "点击了流量统计");
					break;
				case 5:
					Log.i(TAG, "点击了病毒查杀");
					break;
				case 6:
					Log.i(TAG, "点击了系统优化");
					break;
				case 7:
					Log.i(TAG, "点击了高级工具");
					intent = new Intent(HomeActivity.this,
							AToolsActivity.class);
					startActivity(intent);
					break;
				case 8:
					Log.i(TAG, "点击了设置中心");
					intent = new Intent(HomeActivity.this,
							SettingActivity.class);
					startActivity(intent);
					break;

				}

			}
		});
	}

	private EditText et1_dialog_first;
	private EditText et2_dialog_first;
	private Button bt_sure_first_dialog;
	private Button bt_cancle_first_dialog;
	private AlertDialog alertDialog;
	private EditText et_dialog_normal;
	private Button bt_sure_normal_dialog;
	private Button bt_cancle_normal_dialog;

	/**
	 * 进行密码设置
	 */
	protected void showFirstDialog() {
		AlertDialog.Builder builder = new Builder(this);
		View view = View.inflate(this, R.layout.dialog_first_entry, null);
		et1_dialog_first = (EditText) view.findViewById(R.id.et1_dialog_first);
		et2_dialog_first = (EditText) view.findViewById(R.id.et2_dialog_first);
		bt_sure_first_dialog = (Button) view.findViewById(R.id.bt_sure_first_dialog);
		bt_cancle_first_dialog = (Button) view.findViewById(R.id.bt_cancle_first_dialog);
		bt_sure_first_dialog.setOnClickListener(this);
		bt_cancle_first_dialog.setOnClickListener(this);
		alertDialog = builder.create();
		alertDialog.setView(view, 0, 0, 0, 0);
		alertDialog.show();

	}

	/**
	 * 密码验证 进入手机 防盗界面
	 */
	protected void showNomarlDialog() {
		AlertDialog.Builder builder = new Builder(this);
		View view = View.inflate(this, R.layout.dialog_normal_entry, null);
		et_dialog_normal = (EditText) view.findViewById(R.id.et_dialog_normal);
		bt_cancle_normal_dialog = (Button) view.findViewById(R.id.bt_cancle_normal_dialog);
		bt_sure_normal_dialog = (Button) view.findViewById(R.id.bt_sure_normal_dialog);
		bt_sure_normal_dialog.setOnClickListener(this);
		bt_cancle_normal_dialog.setOnClickListener(this);
		alertDialog = builder.create();
		alertDialog.setView(view, 0, 0, 0, 0);
		alertDialog.show();

	}

	/**
	 * 
	 * @return 是否在SharedPreferences 保存密码
	 */
	private boolean isSetPwd() {
		String pwd = sp.getString("pwd", "");
		if (TextUtils.isEmpty(pwd)) {
			return false;
		} else {
			return true;
		}
	}

	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_sure_first_dialog:
			String pwd = et1_dialog_first.getText().toString();
			String pwd2 = et2_dialog_first.getText().toString();
			if(TextUtils.isEmpty(pwd)||TextUtils.isEmpty(pwd2)){
				Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
				return ;
			}
			if(!pwd.equals(pwd2)){
				Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
				return ;
			}
			Editor et = sp.edit();
			et.putString("pwd", pwd);
			et.commit();
			alertDialog.dismiss();

			break;
		case R.id.bt_cancle_first_dialog:
			alertDialog.dismiss();
			break;
		case R.id.bt_cancle_normal_dialog:
			alertDialog.dismiss();
			break;
		case R.id.bt_sure_normal_dialog:
			String passwd = et_dialog_normal.getText().toString();
			if(sp.getString("pwd", "").equals(passwd)){
				Intent intent = new Intent(HomeActivity.this, LostActivity.class);
				startActivity(intent);
				alertDialog.dismiss();
			}else{
				Toast.makeText(this, "你知道密码么？", Toast.LENGTH_SHORT).show();
				et_dialog_normal.setText("");
			}
			
			break;

		}

	}
}
